<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #allmap{width:100%;height:460px;}
        #l-map{width:100%;height: 320px}
        p{margin-left:5px; font-size:14px;}

    </style>
    <style type="text/css">
        .tangram-suggestion-main {
            z-index: 99999999999;
        }
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=zL2pUKzbukIhC6803r0l8gIvxpTKXtLG"></script>

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页<small>公司分布图</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <el-tabs type="border-card">
        <el-tab-pane label="分布图">
            <div id="allmap"></div>
        </el-tab-pane>

        <el-tab-pane label="详细信息">
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                        <span onclick="one()"><el-button type="primary" class="butT" @click="handleCreate()">新建</el-button></span>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="序号" label="序号"></el-table-column>
                        <el-table-column prop="address" label="公司地址" align="center"></el-table-column>
                        <el-table-column prop="lng" label="所在经度" align="center"></el-table-column>
                        <el-table-column prop="lat" label="所在纬度" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                                class="pagiantion"
                                @current-change="handleCurrentChange"
                                :current-page="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog id="demo" title="新增地址"  :visible.sync="dialogFormVisible">
                            <el-input  type="text" id="suggestId" placeholder="公司地址" v-model="address" style="width: 460px;" class="filter-item"></el-input>
                            <el-button type="primary" class="butT" @click="handleAdd()">确认添加</el-button>
                            <div id="l-map"  ></div>
                            <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </el-tab-pane>
    </el-tabs>

</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var lng;
    var lat;
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            address:""
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();//当Vue对象初始化后调用查询方法,展示数据
        },
        methods: {

            //添加
            handleAdd () {
                var address = $("#suggestId").val();
                var data = {
                    address:address,
                    lng: lng,
                    lat: lat
                };
                //发送ajax请求，将请求数据提交到后台
                axios.post("/map/add.do",data).then((res)=>{
                    //关闭新增窗口
                    this.dialogFormVisible = false;
                    if (res.data.flag){
                        //成功。关闭新增窗口
                        this.dialogFormVisible = false;
                        //新增成功后，重新调用分页查询方法，查询新的数据
                        this.findPage();
                        //弹出提示信息
                        this.$message({
                            message:res.data.message,
                            type:'success'
                        });
                    }else {
                        //失败
                        this.$message.error(res.data.message);
                    }
                });

            },
            //分页查询
            findPage(page) {
                if (page == 1){
                    this.pagination.currentPage = page;
                }
                //发送ajax请求，提交相关参数(页码，每页显示记录数，查询条件)
                var param= {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/map/findPage.do",param).then((res)=>{
                    //解析controller响应回的数据，并为模型数据复制
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                });
            },
            // 弹出添加窗口
            handleCreate() {
                this.address = "";
                //弹出新增窗口
                this.dialogFormVisible = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //重新调用分页查询方法，并重新设置页码
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除当前数据吗？",'提示',{
                    type: 'warning'
                }).then(() => {
                    //调用时发送异步求
                    axios.delete("/map/deleteById.do?id="+row.id).then((res)=>{
                        if (res.data.flag){
                            //新增成功后，重新调用分页查询方法，查询新的数据
                            this.findPage();
                            //弹出提示信息
                            this.$message({
                                message:res.data.message,
                                type:'success'
                            });
                        }else {
                            //失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((res)=>{
                        this.showMessage(res);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //刷新页面
            refresh(){
                alert(11);
            }

        },
    });
</script>
<script>
    // 百度地图API功能
    var map = new BMap.Map("allmap");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(116.412222,39.912345), 13);  // 初始化地图,设置中心点坐标和地图级别
    //添加地图类型控件
    map.addControl(new BMap.MapTypeControl({
        mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));
    map.setCurrentCity("上海");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    axios.post("/map/findAll.do").then(res=>{
        //发送请求获取公司地址信息
        var data_info = res.data.data;
        for(var i=0;i<data_info.length;i++) {
            var address = data_info[i].address;//公司地址
            var lng = data_info[i].lng;//经度
            var lat = data_info[i].lat;//维度
            var point = new BMap.Point(lng, lat);
            var marker = new BMap.Marker(point);  // 创建标注
            map.addOverlay(marker);// 将标注添加到地图中
            var opts = {
                position: point,    // 指定文本标注所在的地理位置
                //offset   : new BMap.Size(-90, -50)    //设置文本偏移量
            };
            var label = new BMap.Label(address, opts);  // 创建文本标注对象
            label.setStyle({
                color: "black",
                fontSize: "20px",
                height: "20px",
                lineHeight: "20px",
                fontFamily: "楷体"
            });
            map.addOverlay(label);
        }
    });
</script>
<script>
    function one() {
        window.setTimeout(function () {
            // 百度地图API功能
            function G(id) {
                return document.getElementById(id);
            }


            var map = new BMap.Map("l-map");
            map.centerAndZoom("北京",12);// 初始化地图,设置城市和地图级别。

            var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                {"input" : "suggestId"
                    ,"location" : map
                });

            ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                var str = "";
                var _value = e.fromitem.value;
                var value = "";
                if (e.fromitem.index > -1) {
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                value = "";
                if (e.toitem.index > -1) {
                    _value = e.toitem.value;
                    value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                }
                str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                G("searchResultPanel").innerHTML = str;
            });

            var myValue;
            ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                var _value = e.item.value;
                myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                setPlace();
            });

            function setPlace(){
                map.clearOverlays();    //清除地图上所有覆盖物
                function myFun(){
                    var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    map.centerAndZoom(pp, 12);
                    var marker = new BMap.Marker(pp);
                    map.addOverlay(marker);    //添加标注
                    lng = marker.point.lng;
                    lat = marker.point.lat;
                }
                var local = new BMap.LocalSearch(map, { //智能搜索
                    onSearchComplete: myFun
                });
                local.search(myValue);
            }
        },1)
    }
</script>
</html>